﻿package com.calefk.core {
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	/**
	 * Clase que contiene un movieclip , se usa para mostrar los objetos en pantalla
	 */
	public class Entity extends ClassBase {
		
		protected var mClip:MovieClip;
		protected var mNameAnimation:String;
        protected var mProject:Project;
		
		public function get clip():MovieClip { return mClip; }
		public function get x():Number { return mClip.x; }
		public function get y():Number { return mClip.y; }
		
		public function set x(value:Number):void {
			mClip.x = value;
		}
		
		public function set y(value:Number):void {
			mClip.y = value;
		}
		// constructor
		public function Entity() {
			
		}
		
		override public function update(dt:int):void {
			
		}
		
		public function gotoFrame(name:String):void {
			if (name != mNameAnimation) {
				mClip.gotoAndStop(name);
				mNameAnimation = name;
			}
		}
		
		public function move(vx:Number , vy:Number):void {
			x += vx;
			y += vy;
		}
		// agregar funcionalidad click , cuando se queira borrar solo
		// se pone un valor nulo
		public function set onClick(value:Function):void {
			// asignar el listener nuevo
			if (value == null) {
				// remover el listener si lo tiene
				delete mClip.onClick;
				mClip.removeEventListener(MouseEvent.CLICK , onClickInternal); 
			} else {
				mClip.onClick = value;
				mClip.addEventListener(MouseEvent.CLICK , onClickInternal , false , 0 , true);
			}
		}
		
		private function onClickInternal(e:MouseEvent):void {
			mClip.onClick(this);
		}
		
		override public function clean():void {
			// remover todos los eventos si lo tiene
			delete mClip.onClick;
			mClip.removeEventListener(MouseEvent.CLICK , onClickInternal); 
			// set reference null properties
			mNameAnimation = null;
		}
	}
}